<?php

class User extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'tbl_user':
	 * @var integer $id
	 * @var string $username
	 * @var string $password
	 * @var string $salt
	 * @var string $email
	 * @var string $profile
	 */
	private $_admin;
	public $verifyCode;
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, email', 'required'),
			array('username, password, email', 'length', 'max'=>128),
			array('profile', 'safe'),
			array('username', 'unique', 'message' => "This Username already exists."),
			array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => "only Char and numbers are allowed"),
			array('email', 'unique', 'message' => "This email address already exists in the application"),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'profile' => 'Profile',
			'firstname' => 'First Name',	
			'lastname' => 'Last Name',				
		);
	}
	public function checkPassword($password)
	{
		return $password===$this->password;
	}
	/**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password,$salt)
	{
		/* 	You should pass the entire results of crypt() as the salt for comparing a
   			password, to avoid problems when different hashing algorithms are used. (As
   			it says above, standard DES-based password hashing uses a 2-character salt,
   			but MD5-based hashing uses 12.) */
   			
		return crypt($password, $this->generateSalt()) === $this->password;
		//return $this->hashPassword($password,$salt)===$this->password;
	}
        /**
         * Return admin status.
         * @return boolean
         */
        public function isAdmin() {
                if(Yii::app()->user->isGuest)
                        return false;
                else {
                        if (!isset($this->_admin)) {
                        		
                                if($this->superuser)
                                        $this->_admin = true;
                                else
                                        $this->_admin = false;  
                        }
                        return $this->_admin;
                }
        }
        
	/**
	 * Generates the password hash.
	 * @param string password
	 * @param string salt
	 * @return string hash
	 */
	public function hashPassword($password,$salt)
	{
		// man sollte NIE mit md5 verschlüsseln crypt ist die Sache der Zeit
		//return md5(sha1($salt).$password);
		return crypt ($password, $salt);  //5.6.2012
	}
	
	public function newPassword($password)
	{
        	$this->salt = $this->generateSalt();
        	$this->password = $this->hashPassword($password,$this->salt);
        	$this->activkey ='';
			$this->save();
	}

	/**
	 * Generates a salt that can be used to generate a password hash.
	 * @return string the salt
	 */
	protected function generateSalt()
	{
		return '$1$8shA7bX4$e43aUzA5ugAB405H8R8To0';
	}
	
	/**
	 * Generates a salt that can be used to generate a password hash.
	 * @return string the salt
	 */	
	public function beforeSave() {
    	if ($this->isNewRecord){
        	$this->salt = $this->generateSalt();
        	$this->password = $this->hashPassword($this->password,$this->salt);
        	$this->activkey = md5(sha1('email').$this->email); //$this->hashPassword($this->email, 'email');
        	$this->createtime = time();
        	}
 
    	return parent::beforeSave();
	}
	
	/**
	 * Generates the last vist for the user.
	 * @return null
	 */	
	 
	public function setLastVisit() {
		$this->lastvisit = time();
 		$this->save();
	}
}